package com.inspectortime.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class ValidateLoggedInUserInterceptor implements HandlerInterceptor {

	private String redirectPath;

	private String message;

	public void setMessage(String message) {
		this.message = message;
	}

	public void setRedirectPath(String redirectPath) {
		this.redirectPath = redirectPath;
	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		if (RequestPathUtils.isProtectedPath(request)
				&& !HttpSessionUtils.isUserLoggedIn(request)) {
			response.sendRedirect(request.getContextPath() + this.redirectPath + "?message=" + message);
			return false;
		}
		return true;
	}

}
